<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<% String paramType = request.getParameter("paramType"); %>
<% String paramId = request.getParameter("paramId"); %>
  <head>
    <title>参数值添加页面</title>
    <script type="text/javascript">
    	var paramType = '<%=paramType %>';
    	var paramId = '<%=paramId %>';
    	
    	var _initSelectType = function() {
    		var selectTypes = $('#paramValSelect select');
    		$.each(selectTypes, function(index, selectType) {
    			if ($(selectType).attr("id") == paramType) {
    				$(selectType).show();
    			} else {
    				$(selectType).hide();
    			}
    		});
    	}
    	var _init = function() {
    		if (paramType == "S00") {
    			$('#paramValInput').show();
    			$('#paramValSelect').hide();
    		} else {
    			$('#paramValSelect').show();
    			$('#paramValInput').hide();
    			_initSelectType();
    		}
    	}
    	
    	var _savaDefautValue = function(paramValue, paramText) {
	    	var paramTable = $('table[id="p_param_' + paramId + '"]');
			var valueArr = paramValue.split("|");
    		var valueTextArr = paramText.split("|");
			var defaultValue = $(paramTable).find("select[id='param_def_value']");
			$(defaultValue).html("");
			for (var i = 0; i < valueArr.length; i++) {
				var value = valueArr[i];
				var valueText = valueTextArr[i];
				var opt = $("<option>").text(valueText).attr("value", value);
				$(defaultValue).append(opt);
			}
    	}
    	var _ifNewValueExists = function(value, newValue) {
    		var valueArr = value.split("|");
    		var ifExists = false;
    		for (var i = 0; i < valueArr.length; i++) {
    			if (newValue == valueArr[i]) {
    				ifExists = true;
    				break;
    			}
    		}
    		return ifExists;
    	}
    	var doSave = function() {
    		var paramTable = $('table[id="p_param_' + paramId + '"]');
    		var paramValue = $(paramTable).find("textarea[id='param_value']").attr("val").trim();
    		var paramText = $(paramTable).find("textarea[id='param_value']").attr("value").trim();
    		var newParamText = "";
    		var newParamValue = "";
    		if (paramType == "S00") {//输入类型
    			newParamText = $('#newInputVal').val().trim();
    			newParamValue = $('#newInputVal').val().trim();
    			if (!newParamText) {
    				alert("输入的值不能为空！");
    				return;
    			}
    		} else {//选择
    			newParamText = $('#' + paramType + ' option:selected').text().trim();
    			newParamValue = $('#' + paramType + ' option:selected').val().trim();
    		}
    		if (!paramValue || paramValue == "") {
				paramValue = newParamValue;
				paramText = newParamText;
			} else {
				if (_ifNewValueExists(paramValue, newParamValue)) {
	    			alert("您所输入的值已存在，请重新输入！");
	    			return;
	    		}
				paramValue = paramValue + "|" + newParamValue;
				paramText = paramText + "|" + newParamText;
			}
			$(paramTable).find("textarea[id='param_value']").attr("value", paramText);
			$(paramTable).find("textarea[id='param_value']").attr("val", paramValue);
			
			_savaDefautValue(paramValue, paramText);
			doCancel();
    	}
    	var doCancel = function() {
    		$('#addParamValue').window('close');
    	}
    	_init();
    </script>
  </head>
    <style type="text/css">
		.p_c_packParam {
			background-color:#EDF2F5;
			font-family:'新宋体';
			font-size:12px;
			line-height:30px;
			font-weight:noml;
			overflow:hidden;
			padding:20px 5px;
		}
	</style>
  <body>
    <div id="paramValueContent" class="p_c_packParam">
    	<div id="paramValSelect">
    		请选择参数值：
    		<select id="S03">
    			<option value="2">预付</option>
    			<option value="1">后付</option>
    		</select>
    		<select id="S01">
    			<option value="Y">新装</option>
    			<option value="N">已有</option>
    			<option value="D">不需要</option>
    		</select>
    	</div>
    	<div id="paramValInput">请输入参数值：<input id="newInputVal" type="text"/></div>
    	 <div class="btn_query">
			<input type="button" class="btn-blue" onclick="doSave();" value="确定" />
			<input type="button" class="btn-gray" onclick="doCancel();" value="取消"/>
		</div>
    </div>

  </body>
</html>